package org.noote.LineDetection;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

public class DetectView extends View {
	
	LineDetection _detect;
	
	Bitmap _bitmap;
    Paint _paint = new Paint();	
	int _height = 0;
	int _width = 0;
	
	int _iToWatch = 11;
	
	public DetectView(Context context) {
        super(context);
        
        _detect = new LineDetection();
    }
	
	@Override
	protected void onSizeChanged(int width, int height, int width_old, int height_old)
	{
		super.onSizeChanged(width, height, width_old, height_old);
		
	    _width = width;
	    _height = height;
	 
	    if(_detect.getWidth() != _width || _detect.getHeight() != _height)
	    {
			Log.d(VIEW_LOG_TAG, "onSizeChanged() "+_width+"x"+_height);
			
	    	_bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
	    	_detect.createBuffer(_width, _height);
	    }
	}

	@Override
	protected void onDraw(Canvas canvas) {
		_paint.setColor(Color.WHITE);
		_paint.setStyle(Paint.Style.STROKE);
		_paint.setShadowLayer(2, 0, 0, Color.DKGRAY);

		canvas.drawBitmap(_bitmap, 0, 0, _paint);
	    
		// show some result
		String sMsgShow;
		switch(_iToWatch)
		{
		default:
		case 0:
			sMsgShow = "input";
			break;
		case 1:
			sMsgShow = "intensity";
			break;
		case 2:
			sMsgShow = "sobel";
			break;
		case 3:
			sMsgShow = "edges";
			break;
		case 4:
			sMsgShow = "input+edges";
			break;
		case 5:
			sMsgShow = "orientation";
			break;
		case 6:
			sMsgShow = "input+orientation";
			break;
		case 7:
			sMsgShow = "edgels("+_detect._allransacedgels.size()+")";
			break;
		case 8:
			sMsgShow = "edgels("+_detect._allransacedgels.size()+")+segments("+_detect._allransacsegments.size()+")";
			break;
		case 9:
			sMsgShow = "intensity+segments("+_detect._allransacsegments.size()+")";
			break;
		case 10:
			sMsgShow = "merged segments("+_detect._allmergedsegments.size()+")";
			break;
		case 11:
			sMsgShow = "input+merged segments("+_detect._allmergedsegments.size()+")";
			break;
		}

		canvas.drawText("Size : "+_detect.getWidth()+"x"+_detect.getHeight(), 10, 20, _paint);
		canvas.drawText("Show : "+sMsgShow, 100, 20, _paint);
		canvas.drawText("Duration : "+_detect._stat_duration+"ms", 10, 35, _paint);
		canvas.drawText("  Sobel : "+_detect._stat_sobel+"ms", 10, 50, _paint);
		canvas.drawText("  Ransac : "+_detect._stat_ransac+"ms", 10, 65, _paint);
		canvas.drawText("  Merge : "+_detect._stat_merge+"ms", 10, 80, _paint);
	}

	private void drawPixels()
	{
		// Set modified buffer !
		_bitmap.setPixels(_detect._output.getBuffer(), 0, _detect._output.getWidth(), 0, 0, _bitmap.getWidth(), _bitmap.getHeight());
		
		invalidate();
	}

	public void detect()
	{
		_detect.detect();
	}
	
	public void showNext() {
		_iToWatch = (_iToWatch+1)%12;
		show();
	}
	
	public void show()
	{
		// show some result
		switch(_iToWatch)
		{
		default:
		case 0:
			_detect.print_input();
			break;
		case 1:
			_detect.print_intensity();
			break;
		case 2:
			_detect.print_sobel();
			break;
		case 3:
			_detect.print_black();
			_detect.show_edges();
			break;
		case 4:
			_detect.print_input();			
			_detect.show_edges();
			break;
		case 5:
			_detect.print_black();			
			_detect.show_orientation();
			break;
		case 6:
			_detect.print_input();			
			_detect.show_orientation();
			break;
		case 7:
			_detect.print_black();
			_detect.show_ransac_grids();
			_detect.show_ransac_edgels();			
			break;
		case 8:
			_detect.print_black();
			_detect.show_ransac_grids();
			_detect.show_ransac_edgels();
			_detect.show_ransac_segments();
			break;
		case 9:
			_detect.print_intensity();
			_detect.show_ransac_grids();
			_detect.show_ransac_segments();
			break;
		case 10:
			_detect.print_black();
			_detect.show_segments();
			break;
		case 11:
			_detect.print_input();
			_detect.show_segments();
			break;
		}

		// update le resultat :)
		drawPixels();
	}

	public void clear() {
		_detect.print_black();
		_detect.clearTime();
		drawPixels();
	}
}
